/*
 * @Author: silence
 * @Date: 2022-04-27 11:47:26
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-10-16 10:27:48
 * @Description: 页面基类
 */

import 'package:flutter/material.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import '../../../utils/color/ColorUtils.dart';

import '../common/widgets/buttons/NavSearchBtn.dart';
import 'SilAppBar.dart';

class SilScaffold extends StatefulWidget {
  /**是否隐藏导航条 */
  final bool navBarHide;

  /**是否隐藏状态栏 */
  final bool stateBarHide;

  /**是否隐藏底部安全区域 */
  final bool bottomSafeHide;

  /**是否隐藏返回按钮 */
  final bool leftBackButtonHide;

  /**左边按钮 */
  final Widget? leadingWidget;

  /**左边按钮图片 */
  final String? leadingImgPath;

  /**标题 */
  final String? title;

  /**title */
  final Widget? titleWidget;

  /**右侧按钮 */
  final Widget? trailingWidget;

  /** 主体 */
  final Widget? body;

  /**背景颜色 */
  final Color? backgroundColor;

  /**底部导航条 */
  final Widget? bottomNavigationBar;
  final Widget? floatingActionButton;
  final FloatingActionButtonLocation? floatingActionButtonLocation;

  /**appbar颜色，默认白色 */
  final Color? appBarColor;

  /**返回按钮事件 */
  final Function? backButtonFunc;

  /**显示搜索按钮，默认不显示 */
  final bool showSearchBtn;

  /**是否自动适配底部Inset,默认为true */
  final bool resizeToAvoidBottomInset;

  /**内容距离屏幕两边的间距,默认26 */
  final double sideMargin;

  /**是否显示设备连接状态状态条，默认不显示 */
  final bool showDeviceConnectStateBar;

  const SilScaffold(
      {Key? key,
      this.navBarHide = false,
      this.bottomSafeHide = false,
      this.leftBackButtonHide = false,
      this.title,
      this.titleWidget,
      this.leadingWidget,
      this.trailingWidget,
      this.body,
      this.backgroundColor,
      this.stateBarHide = false,
      this.bottomNavigationBar,
      this.floatingActionButton,
      this.floatingActionButtonLocation,
      this.appBarColor,
      this.backButtonFunc,
      this.showSearchBtn = false,
      this.resizeToAvoidBottomInset = true,
      this.leadingImgPath,
      this.sideMargin = 20,
      this.showDeviceConnectStateBar = false})
      : super(key: key);

  @override
  State<SilScaffold> createState() => _SilScaffoldState();
}

class _SilScaffoldState extends State<SilScaffold> {
  @override
  Widget build(BuildContext context) {
    return MediaQuery.removePadding(
      removeTop: widget.stateBarHide,
      removeBottom: widget.bottomSafeHide,
      context: context,
      child: Scaffold(
          resizeToAvoidBottomInset: widget.resizeToAvoidBottomInset,
          backgroundColor: widget.backgroundColor ?? ColorUtils.color_bg,
          appBar: SilAppBar(
            navBarHeight: (widget.navBarHide == true) ? 0 : 44.w,
            backgroundColor: widget.appBarColor ?? ColorUtils.nav_bar_bg,
            leadingWidget: widget.leadingWidget ?? backButton(),
            titleWidget: widget.titleWidget ?? titleView(),
            trailingWidget: widget.trailingWidget ?? defaultTrailingWidget(),
          ),
          body: Column(
            children: [
              Expanded(
                  child: Container(
                padding: EdgeInsets.symmetric(horizontal: widget.sideMargin.w),
                child: widget.body,
              ))
            ],
          ),
          bottomNavigationBar: widget.bottomNavigationBar,
          floatingActionButton: widget.floatingActionButton,
          floatingActionButtonLocation: widget.floatingActionButtonLocation),
    );
  }

  /**
   * @description: 默认返回按钮
   * @param {*}
   * @return {*}
   */
  Widget backButton() {
    return (widget.leftBackButtonHide == true)
        ? Container()
        : GestureDetector(
            onTap: () {
              if (widget.backButtonFunc != null) {
                widget.backButtonFunc!();
              } else {
                // 如果可以返回，就返回呗
                if (Navigator.canPop(context)) Navigator.pop(context);
              }
            },
            behavior: HitTestBehavior.translucent,
            child: Container(
              margin: EdgeInsets.only(left: 20.w),
              alignment: Alignment.center,
              width: 32.w,
              height: 32.w,
              child: SilImage(
                widget.leadingImgPath ?? 'imgs/nav/nav_icon_white.png',
                width: 24.w,
                height: 24.w,
              ),
            ));
  }

  /**
   * @description: 默认标题
   * @param {*}
   * @return {*}
   */
  Widget titleView() {
    return SilText(widget.title ?? "",
        textDirection: TextDirection.ltr,
        style: TextStyle(
            fontSize: 18.sp,
            color: ColorUtils.nav_bar_title,
            fontWeight: FontWeight.w500));
  }

  /**
   * @description: 右侧按钮
   * @param {*}
   * @return {*}
   */
  Widget defaultTrailingWidget() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      child: Row(children: [
        if (widget.showSearchBtn)
          Padding(
            padding: EdgeInsets.only(right: 12.w),
            child: NavSearchBtn(),
          ),
      ]),
    );
  }

  @override
  void initState() {
    super.initState();
  }
}

class SilScaffoldButton {
  //静态返回
  static Widget backButtonStatic(BuildContext context,
      {String? img = 'imgs/nav/nav_icon_white.png',
      bool full = false,
      void Function()? onTap}) {
    return full
        ? SizedBox()
        : GestureDetector(
            onTap: () {
              // 如果可以返回，就返回呗
              if (onTap != null) {
                onTap();
              } else {
                if (Navigator.canPop(context)) Navigator.pop(context);
              }
            },
            behavior: HitTestBehavior.translucent,
            child: Container(
              margin: EdgeInsets.only(left: 20.w),
              alignment: Alignment.center,
              width: 32.w,
              height: 32.w,
              child: SilImage(
                'imgs/nav/nav_icon_white.png',
                width: 24.w,
                height: 24.w,
              ),
            ));
  }

  /**
   * @description: 默认标题
   * @param {*}
   * @return {*}
   */
  static Widget titleView({String? title}) {
    return Container(
        alignment: Alignment.center,
        child: SilText(title,
            style: TextStyle(
                fontSize: 18.sp,
                color: ColorUtils.nav_bar_title,
                fontWeight: FontWeight.w500)));
  }
}
